/* eslint-disable react/jsx-key */
import React, { useEffect, useRef, useState } from 'react';
import { Switch, Modal, Button, Form, Popconfirm, message, Empty } from 'antd';
import { connect, useRequest } from 'umi';
import AutoTable from '@/components/AutoTable';
import InitForm from '@/components/InitForm';
import { postFetch } from '@/utils/doFetch';
import { stopsettle, companydetail, company } from '@/services/business';
import { PlusOutlined } from '@ant-design/icons';
import moment from 'moment';
import CompanyName from '@/components/CompanyName';


let Company = (props) => {
  const { global, dispatch } = props,
    defaultFields = {
      company_id: {
        value: null,
        type: 'select',
        title: '代账公司',
        name: ['company_id'],
        required: true,
        options: {
          database: company,
          params: {
            is_all: 1,
            type: -1,
            status: "completed",
          },
        },
        col: { span: 24 },
      },
      license_copy_num: {
        value: 0,
        type: 'inputnumber',
        title: '执照副本数量',
        name: ['license_copy_num'],
        required: true,
      },
      license_original_num: {
        value: 0,
        type: 'inputnumber',
        title: '执照正本数量',
        name: ['license_original_num'],
        required: true,
      },
      id_card: {
        value: null,
        type: 'input',
        title: '身份证',
        name: ['id_card'],
        required: true,
      },
      official_seal_num: {
        value: 0,
        type: 'inputnumber',
        title: '公章数量',
        name: ['official_seal_num'],
        required: true,
      },
      invoice_seal_num: {
        value: 0,
        type: 'inputnumber',
        title: '发票章数量',
        name: ['invoice_seal_num'],
        required: true,
      },
      financial_seal_num: {
        value: 0,
        type: 'inputnumber',
        title: '财务章数量',
        name: ['financial_seal_num'],
        required: true,
      },
      private_seal_num: {
        value: 0,
        type: 'inputnumber',
        title: '私章数量',
        name: ['private_seal_num'],
        required: true,
      },
      revenue_invoice_num: {
        value: 0,
        type: 'inputnumber',
        title: '收入发票数量',
        name: ['revenue_invoice_num'],
        required: true,
      },
      cost_invoice_num: {
        value: 0,
        type: 'inputnumber',
        title: '成本发票数量',
        name: ['cost_invoice_num'],
        required: true,
      },
      remark: {
        value: null,
        type: 'textarea',
        title: '备注',
        name: ['remark'],
        required: false,
        col: {
          span: 24,
        },
      },
    };
  let actionRef = useRef(),
    ChildRef = null;
  const [vs, cvs] = useState(false), //表单显/隐
    [fields, cf] = useState(defaultFields),
    [iftype, ciftype] = useState({}),
    [formRef] = Form.useForm();

  function reload() {
    actionRef?.current?.reload();
    ChildRef?.onRefresh();
  }
  const columns = [
    {
      title: '公司名称',
      width: 260,
      dataIndex: 'company_name',
      key: 'company_name',
      render: (_, row) => (
        <CompanyName
          row={{ ...row, name: row?.company_name ?? row?.name, id: row?.company_id ?? row?.id }}
          reload={reload}
        />
      ),
      search: false,
    },
    {
      title: '执照副本数量',
      dataIndex: 'license_copy_num',
      key: 'license_copy_num',
      search: false,
    },
    {
      title: '执照正本数量',
      dataIndex: 'license_original_num',
      key: 'license_original_num',
      search: false,
    },
    {
      title: '公章数量',
      dataIndex: 'official_seal_num',
      key: 'official_seal_num',
      search: false,
    },
    {
      title: '发票章数量',
      dataIndex: 'invoice_seal_num',
      key: 'invoice_seal_num',
      search: false,
    },
    {
      title: '财务章数量',
      dataIndex: 'financial_seal_num',
      key: 'financial_seal_num',
      search: false,
    },
    {
      title: '私章数量',
      dataIndex: 'private_seal_num',
      key: 'private_seal_num',
      search: false,
    },
    {
      title: '收入发票数量',
      dataIndex: 'revenue_invoice_num',
      key: 'revenue_invoice_num',
      search: false,
    },
    {
      title: '成本发票数量',
      dataIndex: 'cost_invoice_num',
      key: 'cost_invoice_num',
      search: false,
    },
    {
      title: '创建人',
      dataIndex: 'creator_name',
      key: 'creator_name',
      search: false,
    },
    {
      title: '身份证号码',
      dataIndex: 'id_card',
      key: 'id_card',
      search: false,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      key: 'remark',
      search: false,
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      search: false,
      render: (_, row) => {
        return (
          <span>
            {row.status == 'stalled'
              ? '未开始'
              : row.status == 'pending'
                ? '交接中'
                : row.status == 'completed'
                  ? '已完成'
                  : ''}
          </span>
        );
      },
    },
    {
      title: '操作',
      valueType: 'option',
      width: 180,
      render: (text, row, _, action) => extraAction(text, row, _, action),
    },
  ];

  function extraAction(text, record, _, action) {
    return [
      <Popconfirm
        disabled={record.status != 'stalled' || !record.is_company}
        title="确认交接该交接资料？"
        onConfirm={() => {
          dispatch({
            type: 'dossier/handovere',
            payload: { id: record.id },
          }).then((res) => {
            if (res.code == 0) {
              message.success('操作成功');
              reload();
              cvs(false);
            }
          });
        }}
      >
        <a disabled={record.status != 'stalled' || !record.is_company}>交接</a>
      </Popconfirm>,
      <Popconfirm
        disabled={record.status != 'pending' || record.is_company}
        title="确认接收该交接资料？"
        onConfirm={() => {
          dispatch({
            type: 'dossier/confirmhandover',
            payload: { id: record.id },
          }).then((res) => {
            if (res.code == 0) {
              message.success('操作成功');
              reload();
              cvs(false);
            }
          });
        }}
      >
        <a disabled={record.status != 'pending' || record.is_company}>接收</a>
      </Popconfirm>,
      <a
        disabled={record.status != 'stalled' || !record.is_company}
        onClick={() => {
          if (record.status != 'stalled' || !record.is_company) {
            return;
          }
          ciftype({
            val: 'edit',
            id: record.id,
            title: '修改交接资料',
          });
          cf(() => {
            for (let i in defaultFields) {
              defaultFields[i].value = record[i];
              if (i.indexOf('password') != -1) {
                defaultFields[i].hides = true;
              }
            }
            return { ...defaultFields };
          });
          cvs(true);
        }}
      >
        修改
      </a>,
      <Popconfirm
        disabled={record.status != 'stalled' || !record.is_company}
        title="确认删除该交接资料？"
        onConfirm={() => {
          dispatch({
            type: 'dossier/deleteresource',
            payload: record.id,
          }).then((res) => {
            if (res.code == 0) {
              message.success('操作成功');
              reload();
              cvs(false);
            }
          });
        }}
      >
        <a
          disabled={record.status != 'stalled' || !record.is_company}
          style={{ color: record.status != 'stalled' || !record.is_company ? '#ddd' : 'red' }}
        >
          删除
        </a>
      </Popconfirm>,
    ];
  }

  let saveData = (values, fn) => {
    let { dispatch } = props;
    if (iftype.val == 'add') {
      dispatch({
        type: 'dossier/addresource',
        payload: { ...values },
      }).then((res) => {
        if (res.code == 0) {
          message.success('操作成功');
          reload();
          cvs(false);
        }
      });
    } else if (iftype.val == 'edit') {
      dispatch({
        type: 'dossier/editresource',
        payload: { ...values, id: iftype.id },
      }).then((res) => {
        if (res.code == 0) {
          message.success('操作成功');
          reload();
          cvs(false);
        }
      });
    }
  };

  let extra = (
    <div
      className="action"
      title="新增"
      onClick={() => {
        cvs(true);
        cf((fields) => {
          for (let i in defaultFields) {
            defaultFields[i].value = defaultFields[i].type == 'inputnumber' ? 0 : null;
            defaultFields[i].hides = false;
          }
          return { ...defaultFields };
        });
        ciftype({
          val: 'add',
          title: '新增原件交接',
        });
      }}
    >
      <PlusOutlined />
    </div>
  );

  return (
    <div className="nopa autotable">
      <AutoTable
        pagetitle={props.route.name}
        pageextra={extra}
        columns={columns}
        actionRef={actionRef}
        onRef={(node) => (ChildRef = node)}
        path="/wbn-api/resource"
      />

      <Modal
        maskClosable={false}
        title={iftype.title}
        open={vs}
        onCancel={() => cvs(false)}
        footer={false}
        destroyOnClose={true}
      >
        <InitForm
          formRef={formRef}
          fields={fields}
          submitData={(values, fn) => {
            saveData(values, fn);
          }}
          onChange={(changedValues, allValues) => { }}
          submitting={
            props.loading.effects['dossier/addresource'] ||
            props.loading.effects['dossier/editresource'] ||
            !vs
          }
        />
      </Modal>
    </div>
  );
};

export default connect(({ global, loading, user }) => ({
  global,
  loading,
  user
}))(Company);
